
import java.util.Date;

public class AdamTrader {

        public static void main(String args[]) {
        	
        	ScreenScraper ss = new ScreenScraper();
        	String ticker = args[0];
        	String screenShot = ss.getScreenShot(ticker);
        	String initialPrice = ss.getPrice(screenShot, ticker);        	
        	       	
        	Double currentPrice = Double.valueOf(initialPrice);
        	Double startPrice = Double.valueOf(initialPrice);
        	String lastPrice = initialPrice;
        	
        	Boolean invest = true;
        	while(invest) {
            	String price = ss.getPrice(screenShot, ticker);
            	try{
                	currentPrice = Double.valueOf(price);
                	if (!(price.matches(lastPrice))){
                	Date date = new Date();
                	String caps = ticker.toUpperCase();    	
                    System.out.println(caps + "'s current price is $" + currentPrice + " at " + date);
                	}
                	invest = stayIn(startPrice, currentPrice);
                	lastPrice = price;
            	} catch (java.lang.NumberFormatException e) {
            		System.out.println("main()..Cannot format price...Attempting to recover");
            	}

        	}

        }



        public static boolean stayIn(Double startPrice, Double currentPrice) {

        	Boolean stayIn = true;
        	Double highSellPrice = startPrice*1.01;
        	Double lowSellPrice = startPrice*0.99;
        	
        	if (currentPrice>highSellPrice){
        		stayIn = false;
        		System.out.println("SOLD!");
        		System.out.println("Profit of " + (highSellPrice-startPrice) + " made!!");
        	} 
        	
        	if (currentPrice<lowSellPrice){
        		stayIn = false;
        		System.out.println("SOLD...");
        		System.out.println("Loss of " + (lowSellPrice-startPrice));
        	} 
        	
        		return stayIn;
        	}
}
